﻿using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using TMOOC.CM.MobileShop.Business.Base_Manage;
using TMOOC.CM.MobileShop.Entity;
using TMOOC.CM.MobileShop.Util;

namespace TMOOC.CM.MobileShop.Server.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    public class Base_UserLogController : BaseApiController
    {
        #region DI

        public Base_UserLogController(IBase_UserLogBusiness base_UserLogBus
                                     ,IBase_UserBusiness base_UserBus
                                     ,IBase_ActionBusiness base_ActionBus)
        {
            _base_UserLogBus = base_UserLogBus;
            _base_UserBus = base_UserBus;
            _base_ActionBus = base_ActionBus;
        }

        IBase_UserLogBusiness _base_UserLogBus { get; }
        IBase_UserBusiness _base_UserBus { get; }
        IBase_ActionBusiness _base_ActionBus { get; }

        #endregion

        #region 获取

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="condition">查询字段</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<AjaxResult<List<Base_UserLog>>> GetDataList(Pagination pagination, string condition, string keyword)
        {
            var dataList = _base_UserLogBus.GetDataList(pagination, condition, keyword);

            return DataTable(dataList, pagination);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<AjaxResult<Base_UserLog>> GetTheData(string id)
        {
            var theData = _base_UserLogBus.GetTheData(id);

            return Success(theData);
        }

        #endregion

        #region 提交

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data">保存的数据</param>
        [HttpPost]
        public ActionResult<AjaxResult> SaveData(Base_UserLog data)
        {
            AjaxResult res;
            if (data.Id.IsNullOrEmpty())
            {
                data.InitEntity();

                res = _base_UserLogBus.AddData(data);
            }
            else
            {
                res = _base_UserLogBus.UpdateData(data);
            }

            return JsonContent(res.ToJson());
        }

        [HttpPost]
        public void SaveLog(string routeUrl)
        {
            //_actionBus.GetDataList(new Pagination(), keyword, null, new List<int> { 2 });
            var data =  _base_ActionBus.GetDataList(new Pagination(), routeUrl);
            
            _base_UserLogBus.SaveLog(JsonConvert.SerializeObject(data),_base_UserBus);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids">id数组,JSON数组</param>
        [HttpPost]
        public ActionResult<AjaxResult> DeleteData(string ids)
        {
            var res = _base_UserLogBus.DeleteData(ids.ToList<string>());

            return JsonContent(res.ToJson());
        }

        #endregion
    }
}